<?php

namespace App\Modules\Admin\Services\Property;

use App\Exceptions\Exception;
use App\Modules\Admin\Entities\Property\CheckCategoryModel;
use App\Modules\Admin\Entities\Property\CheckItemModel;
use App\Modules\Admin\Services\BaseService;
use http\Exception\InvalidArgumentException;
use Illuminate\Support\Facades\DB;

class CheckItemService extends BaseService
{

    public function __construct(CheckItemModel $master)
    {
        $this->model = $master;
        $this->CheckCategoryModel = new CheckCategoryModel();
        $this->with=['category_info'];
//        $this->detailWith=['category_info'];

    }

    public function lists(array $params): array
    {

        $params['where_callback'] = function($query) use ($params){
            $request = request();
            // 按照名称进行搜索
            if (!empty($search = $request->input('search', ''))){
                $query->where('meter_name', 'LIKE', '%' . trim($search) . '%');
            } // apparatus_id
            if (!empty($category_id = $request->input('category_id', ''))){
                $query->where('category_id', $category_id);
            }

            // 是否公开
            $status = $request->input('status', -1);
            if ($status > -1){
                $query->where('status', '=', $status);
            }
        };
        return parent::lists($params); // TODO: Change the autogenerated stub
    }

    public function getSelectLists($request)
    {

        return $this->model->get()->toArray();
//        $items = $this->CheckCategoryModel->with(['items'])->get();
//
//        exit(json_encode($items));
//        $items = $this->CheckCategoryModel->withCount('items')
//            ->orderBy('category_id')
//            ->get()
//            ->mapToGroups(function ($item) {
//                exit(json_encode($item));
//                return [
//                    'main' => $item->only(['category_id', 'category_name']),
//                    'items' => $item->items->map->only(['item_id', 'item_title'])
//                ];
//            });
////        $items  = $this->model->with('category')->get(['item_id','item_name','category_id'])->toArray();
//        exit(json_encode($items));
//        exit(json_encode(array_column($items,'item_id')));
//
//        return [];
    }

    public function create(array $params)
    {
        DB::beginTransaction();
        try{
            parent::create($params); // TODO: Change the autogenerated stub
            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }

    public function update(array $params)
    {
        DB::beginTransaction();
        try{
            parent::update($params); // TODO: Change the autogenerated stub

            DB::commit();
            return true;
        }catch (Exception $e){
            DB::rollBack();
            throw new Exception($e->getMessage());
        }
    }



    public function delete(array $params)
    {
        $primaryKey = $this->model->getKeyName();
        if (isset($params[$primaryKey]) && $params[$primaryKey] == 1){
            throw new Exception('超管禁止删除！');
        }
        // 如果是批量删除，那么移除Id
        if(isset($params['is_batch']) && $params['is_batch'] == 1){
            $ids_ary = explode(',', $params[$primaryKey]);
            if (in_array(1, $ids_ary)){
                $key = array_search(1, $ids_ary);
                unset($ids_ary[$key]);
                $params[$primaryKey] = implode(',', $ids_ary);
            }
        }
        return parent::delete($params); // TODO: Change the autogenerated stub
    }
}
